package JavaFoundation.myLock;

import java.util.concurrent.locks.ReentrantLock;

/*
sync这类实现了AQS
 */
public class AQSTest {
    //AbstractQueueSynchronizer:AQS 抽象的队列同步器
    static ReentrantLock lock = new ReentrantLock();//默认是非公平锁

    public static void main(String[] args) {
        new Thread(()->{
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"/t"+"---开始办理业务");
                try {
                    System.out.println(Thread.currentThread().getName()+"/t"+"开始睡眠");
                    Thread.sleep(50L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }finally {
                lock.unlock();
            }
        },"A").start();

        new Thread(()->{
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"/t"+"---开始办理业务");
            }finally {
                lock.unlock();
            }
        },"B").start();

        new Thread(()->{
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"/t"+"---开始办理业务");
            }finally {
                lock.unlock();
            }
        },"C").start();
    }

}
